export default {
    //localstorage
    local: {
        setItem(key, value, aliveTime) {
            if (key) {
                if (aliveTime) {
                    value = {
                        obj: value,
                        aliveTime: aliveTime,
                        saveTime: new Date().getTime()
                    }
                }
                localStorage.setItem(key,JSON.stringify(value))
            }
        },
        getItem(key) {
            let data;
            try {
                data = JSON.parse(localStorage.getItem(key));
            } catch (e) {}
            if (data) {
                if (data.aliveTime && data.saveTime) {
                    if (new Date().getTime() - data.saveTime >= data.aliveTime) {
                        //超时了
                        this.del(key);
                        return null;
                    } else {
                        return data.obj;
                    }
                } else {
                    return data;
                }
            } else
                return null;
        },
        removeItem(key) {
            if (key)
                localStorage.removeItem(key);
        },
        clear() {
            localStorage.clear();
        }
    },
    //sessionStorage
    session: {
        setItem: (key, value) => {
            if (key)
                sessionStorage.setItem(key,JSON.stringify(value))
        },
        getItem: (key) => {
            let data = sessionStorage.getItem(key);
            if (data)
                return JSON.parse(data);
            else
                return null;
        },
        removeItem: (key) => {
            if (key)
                sessionStorage.removeItem(key);
        },
        clear: () => {
            sessionStorage.clear();
        }
    }
}